package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dto.DiagnosePlanDto;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCompanyFileMapper;
import com.ruoyi.system.domain.SysCompanyFile;
import com.ruoyi.system.service.ISysCompanyFileService;

/**
 * 企业诊断文件Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-14
 */
@Service
public class SysCompanyFileServiceImpl implements ISysCompanyFileService 
{
    @Autowired
    private SysCompanyFileMapper sysCompanyFileMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询企业诊断文件
     * 
     * @param fileId 企业诊断文件主键
     * @return 企业诊断文件
     */
    @Override
    public SysCompanyFile selectSysCompanyFileByFileId(Long fileId)
    {
        SysCompanyFile sysCompanyFile=sysCompanyFileMapper.selectSysCompanyFileByFileId(fileId);
        if(sysCompanyFile!=null){
            if(sysCompanyFile.getCheckorId()!=null){
                SysUser user=userMapper.selectUserById(sysCompanyFile.getCheckorId());
                if(user!=null){
                    sysCompanyFile.setCheckor(user.getNickName());
                }
            }


        }
        return sysCompanyFile;
    }

    /**
     * 查询企业诊断文件列表
     * 
     * @param sysCompanyFile 企业诊断文件
     * @return 企业诊断文件
     */
    @Override
    public List<SysCompanyFile> selectSysCompanyFileList(SysCompanyFile sysCompanyFile)
    {
        return sysCompanyFileMapper.selectSysCompanyFileList(sysCompanyFile);
    }

    /**
     * 新增企业诊断文件
     * 
     * @param sysCompanyFile 企业诊断文件
     * @return 结果
     */
    @Override
    public int insertSysCompanyFile(SysCompanyFile sysCompanyFile)
    {
        SysDept sysDept=deptMapper.selectDeptById(sysCompanyFile.getDeptId());

        if(sysDept!=null){
            if(sysDept.getDiagnosePlan()!=null){
                List<DiagnosePlanDto> diagnosePlanDtos= JSONArray.parseArray(sysDept.getDiagnosePlan(),DiagnosePlanDto.class);
                boolean isExist=false;
                for (DiagnosePlanDto diagnosePlanDto:diagnosePlanDtos){
                    if(diagnosePlanDto.getWorkNote().equals(sysCompanyFile.getWorkNote())){
                        diagnosePlanDto.setNoteStatus(sysCompanyFile.getNoteStatus());
                        isExist=true;
                        return 0;
                    }
                }
                if(!isExist){
                    DiagnosePlanDto diagnosePlanDto=new DiagnosePlanDto();
                    diagnosePlanDto.setNoteStatus(sysCompanyFile.getNoteStatus());
                    diagnosePlanDto.setWorkNote(sysCompanyFile.getWorkNote());
                    diagnosePlanDtos.add(diagnosePlanDto);
                }

                sysDept.setDiagnosePlan(JSON.toJSONString(diagnosePlanDtos));

            }else{
                List<DiagnosePlanDto> diagnosePlanDtos=new ArrayList<>();
                DiagnosePlanDto diagnosePlanDto=new DiagnosePlanDto();
                diagnosePlanDto.setNoteStatus(sysCompanyFile.getNoteStatus());
                diagnosePlanDto.setWorkNote(sysCompanyFile.getWorkNote());
                diagnosePlanDtos.add(diagnosePlanDto);

                sysDept.setDiagnosePlan(JSON.toJSONString(diagnosePlanDtos));
            }

            deptMapper.updateDept(sysDept);

        }

        sysCompanyFile.setCreateTime(DateUtils.getNowDate());

        return sysCompanyFileMapper.insertSysCompanyFile(sysCompanyFile);
    }

    /**
     * 修改企业诊断文件
     * 
     * @param sysCompanyFile 企业诊断文件
     * @return 结果
     */
    @Override
    public int updateSysCompanyFile(SysCompanyFile sysCompanyFile)
    {
        SysDept sysDept=deptMapper.selectDeptById(sysCompanyFile.getDeptId());

        if(sysDept!=null){
            List<DiagnosePlanDto> diagnosePlanDtos= JSONArray.parseArray(sysDept.getDiagnosePlan(),DiagnosePlanDto.class);
            for (DiagnosePlanDto diagnosePlanDto:diagnosePlanDtos){
                if(diagnosePlanDto.getWorkNote().equals(sysCompanyFile.getWorkNote())){
                    diagnosePlanDto.setNoteStatus(sysCompanyFile.getNoteStatus());
                    break;
                }
            }
            sysDept.setDiagnosePlan(JSON.toJSONString(diagnosePlanDtos));

            deptMapper.updateDept(sysDept);

        }

        sysCompanyFile.setUpdateTime(DateUtils.getNowDate());
        return sysCompanyFileMapper.updateSysCompanyFile(sysCompanyFile);
    }

    @Override
    public int updateAgainSysCompanyFile(SysCompanyFile sysCompanyFile)
    {
        SysDept sysDept=deptMapper.selectDeptById(sysCompanyFile.getDeptId());

        if(sysDept!=null){
            List<DiagnosePlanDto> diagnosePlanDtos= JSONArray.parseArray(sysDept.getDiagnosePlan(),DiagnosePlanDto.class);
            for (DiagnosePlanDto diagnosePlanDto:diagnosePlanDtos){
                if(diagnosePlanDto.getWorkNote().equals(sysCompanyFile.getWorkNote())){
                    diagnosePlanDto.setNoteStatus(sysCompanyFile.getNoteStatus());
                    break;
                }
            }
            sysDept.setDiagnosePlan(JSON.toJSONString(diagnosePlanDtos));

            deptMapper.updateDept(sysDept);

        }
        sysCompanyFile.setReason("");
        sysCompanyFile.setUpdateTime(DateUtils.getNowDate());
        return sysCompanyFileMapper.updateSysCompanyFile(sysCompanyFile);
    }

    /**
     * 批量删除企业诊断文件
     * 
     * @param fileIds 需要删除的企业诊断文件主键
     * @return 结果
     */
    @Override
    public int deleteSysCompanyFileByFileIds(Long[] fileIds)
    {
        return sysCompanyFileMapper.deleteSysCompanyFileByFileIds(fileIds);
    }

    /**
     * 删除企业诊断文件信息
     * 
     * @param fileId 企业诊断文件主键
     * @return 结果
     */
    @Override
    public int deleteSysCompanyFileByFileId(Long fileId)
    {
        return sysCompanyFileMapper.deleteSysCompanyFileByFileId(fileId);
    }
}
